<!--
 * @Author: your name
 * @Date: 2021-04-07 19:40:48
 * @LastEditTime: 2021-05-21 18:39:15
 * @LastEditors: Please set LastEditors
 * @Description: 折线图 样式2
 * @FilePath: /medicine-web/src/components/Charts/LineChart.vue
-->
<template>
  <div class="line-chart-container">
    <div :id="elementId" style="height: 360px;"></div>
  </div>
</template>
<script>
import * as echarts from 'echarts';
export default {
  name: 'lineChart1',
  data () {
    return {}
  },
  mounted () {
    this.initChart()
  },
  methods: {
    initChart () {
      var myChart = echarts.init(document.getElementById(this.elementId))
      myChart.setOption({
        title: {
          text: this.chartTitle,
          textStyle: {
            color: '#555555',
            fontSize: 14,
            fontWeight: 'bold'
          }
        },
        tooltip: {
          trigger: 'axis',
        },
        grid: {
          left: '2%',
          right: '2%',
          bottom: '70px',
          containLabel: true
        },
        legend: {
          show: false
        },
        xAxis: {
          type: 'category',
          data: this.getXAxis(),
          boundaryGap: false,
          axisLine: {
            lineStyle: {
              color: '#D8D8D8'
            }
          },
          axisLabel: {
            color: '#ACACAC'
          }
        },
        yAxis: {
          type: 'value',
          axisLine: {
            show: false
          },
          axisTick: {
            show: false
          },
          splitLine: {
            lineStyle: {
              color: ['#E6E6E6']
            }
          }
        },
        series: [
          {
            data: this.getSeriesData(),
            type: 'line',
            smooth: true,
            itemStyle: {
              color: '#68DFF5'
            }
          }
        ]
      })
    },
    getXAxis () {
      return Array.from(new Set(this.chartData.map(item => item.name)))
    },
    getSeriesData () {
      return Array.from(this.chartData.map(item => item.data))
    }
  },
  props: {
    elementId: {
      type: String,
      default: ''
    },
    chartTitle: {
      type: String,
      default: ''
    },
    chartData: {
      type: Array,
      default: () => []
    },
    isShowLegend: {
      type: Boolean,
      default: true
    }
  },
  watch: {
    chartData (val, old) {
      this.initChart()
    }
  }
}
</script>
<style lang="scss" scoped>
.line-chart-container {
  box-shadow: 5px 5px 6px #ccc;
  border-radius: 5px;
}
</style>